package com.example.idachuappone.index.dialog;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
import antistatic.spinnerwheel.OnWheelChangedListener;
import antistatic.spinnerwheel.OnWheelScrollListener;
import antistatic.spinnerwheel.WheelView;
import antistatic.spinnerwheel.adapters.AbstractWheelTextAdapter;

import com.example.idachuappone.R;
import com.example.idachuappone.cook.entity.WorkDay;
import com.example.idachuappone.utils.Utils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class DialogServiceDateHours {
	private Context context;
	private int[] hourindex;
	private List<String> list3;
	private MyDateTimeWheelViewAdapter myDateTimeWheelViewAdapter;
	private MyWheelViewAdapter2 myWheelViewAdapter2;
	private MyWheelViewAdapter myWheelViewAdapter3;
	private boolean scrolling;
	private WheelView wheelView1;
	private WheelView wheelView2;
	private WheelView wheelView3;
	private int yearindex;

	class AnonymousClass_1 implements OnClickListener {
		private final/* synthetic */CallBack val$callBack;
		private final/* synthetic */List val$list;

		AnonymousClass_1(CallBack callBack, List list) {
			this.val$callBack = callBack;
			this.val$list = list;
		}

		@Override
		public void onClick(View v) {
			this.val$callBack.btnOk(
					(WorkDay) this.val$list.get(DialogServiceDateHours.this.wheelView1.getCurrentItem()),
					DialogServiceDateHours.this.wheelView2.getCurrentItem(),
					DialogServiceDateHours.this.list3.get(DialogServiceDateHours.this.wheelView3.getCurrentItem()));
		}
	}

	public static interface CallBack {
		void btnOk(WorkDay workDay, int i, String str);
	}

	class AnonymousClass_3 implements OnWheelChangedListener {
		private final/* synthetic */List val$list;

		AnonymousClass_3(List list) {
			this.val$list = list;
		}

		@Override
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			if (!DialogServiceDateHours.this.scrolling) {
				DialogServiceDateHours.this.updateCities(DialogServiceDateHours.this.wheelView2, this.val$list,
						newValue);
			}
		}
	}

	class AnonymousClass_4 implements OnWheelScrollListener {
		private final/* synthetic */List val$list;

		AnonymousClass_4(List list) {
			this.val$list = list;
		}

		@Override
		public void onScrollingStarted(WheelView wheel) {
			DialogServiceDateHours.this.scrolling = true;
		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			DialogServiceDateHours.this.scrolling = false;
			DialogServiceDateHours.this.updateCities(DialogServiceDateHours.this.wheelView2, this.val$list,
					DialogServiceDateHours.this.wheelView1.getCurrentItem());
		}
	}

	private class MyDateTimeWheelViewAdapter extends AbstractWheelTextAdapter {
		private List<WorkDay> dateTimes;

		protected MyDateTimeWheelViewAdapter(Context context, List<WorkDay> list) {
			super(context, 2130903109, 0);
			setTextTypeface(Typeface.DEFAULT);
			setItemTextResource(R.id.date_ymd);
			this.dateTimes = list;
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			return super.getItem(index, cachedView, parent);
		}

		@Override
		public int getItemsCount() {
			return this.dateTimes == null ? 0 : this.dateTimes.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			return (this.dateTimes == null || this.dateTimes == null) ? null : this.dateTimes.get(index).getDate();
		}
	}

	private class MyWheelViewAdapter2 extends AbstractWheelTextAdapter {
		private String[] hourList;

		protected MyWheelViewAdapter2(Context context, String[] list) {
			super(context, 2130903109, 0);
			setTextTypeface(Typeface.DEFAULT);
			setItemTextResource(R.id.date_ymd);
			this.hourList = list;
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			return super.getItem(index, cachedView, parent);
		}

		@Override
		public int getItemsCount() {
			return this.hourList == null ? 0 : this.hourList.length;
		}

		@Override
		protected CharSequence getItemText(int index) {
			return (this.hourList == null || this.hourList == null) ? null : this.hourList[index];
		}
	}

	private class MyWheelViewAdapter extends AbstractWheelTextAdapter {
		private List<String> hourList;

		protected MyWheelViewAdapter(Context context, List<String> list) {
			super(context, 2130903109, 0);
			setTextTypeface(Typeface.DEFAULT);
			setItemTextResource(R.id.date_ymd);
			this.hourList = list;
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			return super.getItem(index, cachedView, parent);
		}

		@Override
		public int getItemsCount() {
			return this.hourList == null ? 0 : this.hourList.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			return (this.hourList == null || this.hourList == null) ? null : (String) this.hourList.get(index);
		}
	}

	public DialogServiceDateHours(Context context) {
		this.context = context;
	}

	public Dialog showDateHours(Activity activity, CallBack callBack, List<WorkDay> list) {
		initDateHours(list);
		View view = LayoutInflater.from(this.context).inflate(R.layout.dialog_date_time_main, null);
		((TextView) view.findViewById(R.id.btn_date_hour_ok)).setOnClickListener(new AnonymousClass_1(callBack, list));
		this.wheelView1 = (WheelView) view.findViewById(R.id.wheelView1);
		this.wheelView2 = (WheelView) view.findViewById(R.id.wheelView2);
		this.myWheelViewAdapter2 = new MyWheelViewAdapter2(this.context, list.get(0).getHh());
		this.wheelView2.setViewAdapter(this.myWheelViewAdapter2);
		this.wheelView2.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!DialogServiceDateHours.this.scrolling) {
					DialogServiceDateHours.this.hourindex[DialogServiceDateHours.this.yearindex] = newValue;
				}
			}
		});
		this.myDateTimeWheelViewAdapter = new MyDateTimeWheelViewAdapter(this.context, list);
		this.wheelView1.setViewAdapter(this.myDateTimeWheelViewAdapter);
		this.wheelView1.addChangingListener(new AnonymousClass_3(list));
		this.wheelView1.addScrollingListener(new AnonymousClass_4(list));
		this.wheelView3 = (WheelView) view.findViewById(R.id.wheelView3);
		this.myWheelViewAdapter3 = new MyWheelViewAdapter(this.context, this.list3);
		this.wheelView3.setViewAdapter(this.myWheelViewAdapter3);
		this.wheelView1.setVisibleItems(BitmapCacheManagementTask.MESSAGE_CLOSE);
		this.wheelView2.setVisibleItems(BitmapCacheManagementTask.MESSAGE_CLOSE);
		this.wheelView3.setVisibleItems(BitmapCacheManagementTask.MESSAGE_CLOSE);
		view.setMinimumWidth((int) ((Utils.getScreenWidth(activity)) * 0.9d));
		Dialog dialog = new Dialog(this.context, 2131361801);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);
		Window dialogWindow = dialog.getWindow();
		LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(17);
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);
		return dialog;
	}

	private void updateCities(WheelView city, List<WorkDay> list, int index) {
		this.yearindex = index;
		city.setViewAdapter(new MyWheelViewAdapter2(this.context, list.get(index).getHh()));
		city.setCurrentItem(this.hourindex[index], true);
	}

	private void initDateHours(List<WorkDay> list) {
		this.list3 = new ArrayList();
		this.list3.add("00");
		this.list3.add("30");
		this.hourindex = new int[list.size()];
		for (int i = 0; i < this.hourindex.length; i++) {
			this.hourindex[i] = 0;
		}
	}
}
